﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace Buka.BLL.Model
{
    public class User
    {
        private long _Id;

        public long Id
        {
            get { return _Id; }
            set { _Id = value; }
        }
        private string _UserName;

        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }
        private string _Password;

        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }
        private string _email;

        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }
	
        private string _FirstName;

        public string FirstName
        {
            get { return _FirstName; }
            set { _FirstName = value; }
        }
        private string _LastName;

        public string LastName
        {
            get { return _LastName; }
            set { _LastName = value; }
        }
        
        private string _city;

        public string City
        {
            get { return _city; }
            set { _city = value; }
        }

        private long _ranking;

        public long Ranking
        {
            get { return _ranking; }
            set { _ranking = value; }
        }

        private string _sex;

        public string Sex
        {
            get { return _sex; }
            set { _sex = value; }
        }

        private long _userTypeId;

        public long UserTypeId
        {
            get { return _userTypeId; }
            set { _userTypeId = value; }
        }

        private long _statusId;

        public long StatusId
        {
            get { return _statusId; }
            set { _statusId = value; }
        }
	
        private bool _isActive;

        public bool IsActive
        {
            get { return _isActive; }
            set { _isActive = value; }
        }
      
        private DateTime _endDate;

        public DateTime EndDate
        {
            get { return _endDate; }
            set { _endDate = value; }
        }
        private long _userRightsID;

        public long UserRightsID
        {
            get { return _userRightsID; }
            set { _userRightsID = value; }
        }

        private DateTime _createdDate;

        public DateTime CreatedDate
        {
            get { return _createdDate; }
            set { _createdDate = value; }
        }

        private Nullable<DateTime> _lastLoginDate;

        public Nullable<DateTime> LastLoginDate
        {
            get { return _lastLoginDate; }
            set { _lastLoginDate = value; }
        }

        private bool _isOnline;

        public bool IsOnline
        {
            get { return _isOnline; }
            set { _isOnline = value; }
        }
        private DateTime _birthDate;

        public DateTime BirthDate
        {
            get { return _birthDate; }
            set { _birthDate = value; }
        }

        private string _avatar=string.Empty;

        public string Avatar
        {
            get { return _avatar; }
            set { _avatar= value; }
        }

        private string _personalMessage=string.Empty;

        public string PersonalMssage
        {
            get { return _personalMessage; }
            set { _personalMessage= value; }
        }

        #region Methods

        static internal string DecryptPassword(string CryptedPassword, string KeyString)
        {
            byte[] Input = Convert.FromBase64String(CryptedPassword);
            byte[] key = Encoding.UTF8.GetBytes(KeyString.PadRight(8, ' ').Substring(0, 8));
            byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                try
                {
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write))
                    {
                        cs.Write(Input, 0, Input.Length);
                        cs.FlushFinalBlock();

                        return Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
                catch (CryptographicException ex)
                {
                    throw new Exception("Cryptographic error: " + ex.Message);
                }
                catch (Exception ex)
                {
                    throw new Exception("General error: " + ex.Message);
                }
            }
        }

        static internal string EncryptPassword(string Password, string KeyString)
        {
            byte[] input = Encoding.UTF8.GetBytes(Password);
            byte[] key = Encoding.UTF8.GetBytes(KeyString.PadRight(8, ' ').Substring(0, 8));
            byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write))
                {
                    cs.Write(input, 0, input.Length);
                    cs.FlushFinalBlock();
                    string temp = Convert.ToBase64String(ms.ToArray());
                    return Convert.ToBase64String(ms.ToArray());
                }
            }
        }


        #endregion

    }
}
